package main

import (
	"goAdapter/device"
	"goAdapter/device/commInterface"
	"goAdapter/httpServer"
	"goAdapter/setting"
	"log"
	"os"
	"os/signal"
	"runtime"
	"syscall"
	"time"
	//	_ "net/http/pprof"
)

func main() {

	setting.GetConf()
	setting.InitLogger()
	/**************日志初始化***********************/
	/**************获取配置文件***********************/
	//setting.InitVpn()
	//for true {
	//	time.Sleep(5 * time.Second)
	//}
	//记录起始时间
	setting.GetTimeStart()

	if runtime.GOOS == "linux" && runtime.GOARCH == "mipsle" {
		/**************内置mqtt初始化***********************/
		err := device.InnerMqttInit()
		if err != nil {
			setting.ZAPS.Errorf("连接mqtt失败 %v", err)
			return
		}
	}

	/**************协议初始化****************/
	//protocol.ProInterfaceInit()
	/**************变量模板初始化****************/
	device.DeviceTSLInit()
	/**************通信接口初始化****************/
	commInterface.CommInterfaceInit()

	/**************从机设备初始化****************/
	device.DevSlaveInit()

	//调试使用
	//go runNumGoroutineMonitor()

	//启动公有云服务
	//report.ReportServiceInit()

	httpServer.RouterWeb(setting.HttpPort)
	//httpServer.RouterWeb(":8081")
	if runtime.GOOS == "linux" && runtime.GOARCH == "mipsle" {
		//setting.InitVpn()
	}

	setting.ZAPS.Info("初始化成功，开始连接VPN服务!")

	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
	select {
	case <-signalChan:
		setting.ZAPS.Infof("程序退出")
		return
	}
}

func runNumGoroutineMonitor() {
	log.Printf("协程数量->%d\n", runtime.NumGoroutine())

	for {
		select {
		case <-time.After(time.Second):
			log.Printf("协程数量->%d\n", runtime.NumGoroutine())
		}
	}
}
